﻿using System.Windows;

using EMan.Common.DependencyInjection;
using EMan.DependencyInjection.Scoping;
using EMan.GUI;
using EMan.GUI.EditStructure;
using EMan.Interface.Common;

using TrueCode.Core.LifetimeManagement;

namespace EMan
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow
  {
    private readonly Factory _globalFactory;

    public MainWindow()
    {
      InitializeComponent();

      Application.Current.DispatcherUnhandledException += ( _, e ) => MessageBox.Show( e.Exception.Message );

      _globalFactory = new Factory( GlobalScopeContainerProvider.GetContainer() );
    }

    private void EditStructureButton_OnClick( object sender, RoutedEventArgs e )
    {
      ShowInWindow< StructureTreeEditorView >();
    }

    private void EditJobsButton_OnClick( object sender, RoutedEventArgs e )
    {
      ShowInWindow< JobListEditorView >();
    }

    private void MainViewButton_OnClick( object sender, RoutedEventArgs e )
    {
      ShowInWindow< MainView >();
    }

    private void ShowInWindow< TView >() where TView : IView
    {
      using(var lifetime = new Lifetime( "MainWindow.ShowInWindow", true ))
      {
        var window = new TempWindow( _globalFactory.Resolve< TView >(lifetime) );
        window.ShowDialog();
      }
    }
  }
}